import Vue from 'vue';
import PrivacyPopup from '@/components/privacy-popup/privacy-popup.vue';

const PrivacyService = {
  instance: null,

  // 初始化隐私政策服务
  init () {
    if (this.instance) return;
    const agreed = uni.getStorageSync('privacyAgreed');
    console.log('88888', agreed)
    // 创建隐私政策组件实例
    const PrivacyComponent = Vue.extend(PrivacyPopup);
    this.instance = new PrivacyComponent();

    // 挂载到DOM
    const el = document.createElement('div');
    document.body.appendChild(el);
    this.instance.$mount(el);
  },

  // 显示隐私政策弹窗
  show (vmContext) {
    return new Promise((resolve) => {
      // 确保组件已初始化
      if (!this.instance) {
        this.init();
      }

      // 监听同意事件
      this.instance.$once('agreed', (agreed) => {
        resolve(agreed);
      });

      // 显示弹窗
      this.instance.show();
    });
  },

  // 隐藏隐私政策弹窗
  hide () {
    if (this.instance) {
      this.instance.hide();
    }
  },

  // 销毁隐私政策服务
  destroy () {
    if (this.instance && this.instance.$el && this.instance.$el.parentNode) {
      this.instance.$el.parentNode.removeChild(this.instance.$el);
    }
    this.instance = null;
  }
};

export default PrivacyService;
